package cn.hrlly.tao2shou.biz;

import android.content.Context;

import com.zhy.http.okhttp.OkHttpUtils;

import cn.hrlly.tao2shou.other.Config;
import cn.hrlly.tao2shou.callback.FindPasswordCallback;
import cn.hrlly.tao2shou.callback.GetCodeCallback;
import cn.hrlly.tao2shou.domain.User;
import cn.hrlly.tao2shou.listener.OnFindPasswordListener;
import cn.hrlly.tao2shou.utils.DatabaseUtil;

/**
 * User: gengliang
 * Date: 2016-01-27
 * Time: 10:28
 */

public class FindPasswordBizImpl implements FindPasswordBiz {

    private OnFindPasswordListener onFindPasswordListener;
    private DatabaseUtil databaseUtil;

    public FindPasswordBizImpl(OnFindPasswordListener onFindPasswordListener,Context context){
        this.onFindPasswordListener=onFindPasswordListener;
        this.databaseUtil=new DatabaseUtil(context);
    }

    @Override
    public void getVerificationCode(User user) {
        OkHttpUtils
                .post()
                .url(Config.key_url)
                .addParams(Config.key_action, Config.action_get_veritification_code)
                .addParams(Config.key_phone_number,user.getPhoneNumber())
                .build()
                .execute(new GetCodeCallback(onFindPasswordListener));
    }

    @Override
    public void findPassword(User user) {
        OkHttpUtils
                .post()
                .url(Config.key_url)
                .addParams(Config.key_action,Config.action_find_password)
                .addParams(Config.key_phone_number,user.getPhoneNumber())
                .addParams(Config.key_password,user.getPassword())
                .addParams(Config.key_verifivation_code,user.getVerificationCode())
                .addParams(Config.key_longditude, String.valueOf(user.getLongditude()))
                .addParams(Config.key_lantitude, String.valueOf(user.getLantitude()))
                .build()
                .execute(new FindPasswordCallback(onFindPasswordListener));
    }

    @Override
    public void saveUserInfo(User user) {
        databaseUtil.insertUserInfo(user);
    }

    @Override
    public void closeDataBase() {
        databaseUtil.closeDatabase();
    }
}
